package aupdg.presentation;

import aupdg.logic.analysis.SecurityLevel;
import aupdg.data.model.Edge;
import aupdg.data.model.impl.AuDataDependenceEdge;
import aupdg.data.model.impl.AuLdgData;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

/**
 * Popup menu that appears when one right-clicks on a data-dependence edge.
 *
 * @author AA and CJ
 */
public class EdgePopupMenu extends JPopupMenu implements EdgeMenuListener<Edge> {

    AuDataDependenceEdge edge;
    List<AuLdgData> dataObjects;
    VisualizationViewer vv;

    /**
     * Default constructor.
     */
    public EdgePopupMenu() {
        super("Edge Menu");
    }

    @Override
    public void setView(VisualizationViewer vv) {
        this.vv = vv;
        vv.repaint();
    }

    @Override
    public void setEdge(AuDataDependenceEdge edge) {
        this.edge = edge;

        if (edge != null) {
            this.removeAll();
            this.validate();

            dataObjects = edge.getDataObjects();

            for (int i = 0; i < dataObjects.size(); i++) {
                if (dataObjects.get(i).getSecurityLevel() == SecurityLevel.LOW) {
                    this.add(new DataObjectButtonWithLowSec(dataObjects.get(i)));
                } else {
                    this.add(new DataObjectButtonWithHighSec(dataObjects.get(i)));
                }
            }
        }
    }

    /**
     * Implements an entry on the menu containing a data object label (with high
     * security level) that after clicked sets it to low security level.
     */
    public class DataObjectButtonWithHighSec extends JRadioButtonMenuItem {

        final AuLdgData dataObject;

        /**
         * Constructor.
         *
         * @param dataObject
         */
        public DataObjectButtonWithHighSec(final AuLdgData dataObject) {
            super("(H) " + dataObject.getDataLabel() + ":: set security level LOW");
            this.dataObject = dataObject;

            this.addActionListener(new ActionListener() {

                /**
                 * Changes the security level to low.
                 */
                @Override
                public void actionPerformed(ActionEvent e) {
                    dataObject.setSecurityLevel(SecurityLevel.LOW);
                    vv.repaint();
                }
            });
        }
    }

    /**
     * Implements an entry on the menu containing a data object label (with low
     * security level) that after clicked sets it to high security level.
     */
    public class DataObjectButtonWithLowSec extends JRadioButtonMenuItem {

        final AuLdgData dataObject;

        /**
         * Constructor.
         *
         * @param dataObject
         */
        public DataObjectButtonWithLowSec(final AuLdgData dataObject) {
            super("(L) " + dataObject.getDataLabel() + ":: set security level HIGH");
            this.dataObject = dataObject;

            this.addActionListener(new ActionListener() {

                /**
                 * Changes the security level to high.
                 */
                @Override
                public void actionPerformed(ActionEvent e) {
                    dataObject.setSecurityLevel(SecurityLevel.HIGH);
                    vv.repaint();
                }
            });
        }
    }
}
